home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / if_pyth.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  9.8 KB  |  265 lines

  1. *if_pyth.txt*   For Vim version 6.0.  Last change: 2001 Sep 05
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Paul Moore
  5.  
  6.  
  7. The Python Interface to Vim                *python* *Python*
  8.  
  9. 1. Commands            |python-commands|
  10. 2. The vim module        |python-vim|
  11. 3. Buffer objects        |python-buffer|
  12. 4. Range objects        |python-range|
  13. 5. Window objects        |python-window|
  14.  
  15. {Vi does not have any of these commands}
  16.  
  17. The Python interface is available only when Vim was compiled with the
  18. |+python| feature.
  19.  
  20. ==============================================================================
  21. 1. Commands                        *python-commands*
  22.  
  23.                     *:python* *:py* *E205* *E263* *E264*
  24. :[range]py[thon] {stmt}
  25.             Execute Python statement {stmt}.
  26.  
  27. :[range]py[thon] << {endmarker}
  28. {script}
  29. {endmarker}
  30.             Execute Python script {script}.
  31.  
  32. {endmarker} must NOT be preceded by any white space.  If {endmarker} is
  33. omitted from after the "<<", a dot '.' must be used after {script}, like
  34. for the |:append| and |:insert| commands.
  35. This form of the |:python| command is mainly useful for including python code
  36. in Vim scripts.
  37.  
  38. Example: >
  39.     function! IcecreamInitialize()
  40.         python << EOF
  41.         class StrawberryIcecream:
  42.         def __call__(self):
  43.             print 'EAT ME'
  44.     EOF
  45.     endfunction
  46. <
  47.  
  48.                             *:pyfile* *:pyf*
  49. :[range]pyf[ile] {file}    Execute the Python script in {file}.  {not in Vi}
  50.  
  51. Both of these commands do essentially the same thing - they execute a piece of
  52. Python code, with the "current range" |python-range| set to the given line
  53. range.
  54.  
  55. In the case of :python, the code to execute is in the command-line.
  56. In the case of :pyfile, the code to execute is the contents of the given file.
  57.  
  58. Python commands cannot be used in the |sandbox|.
  59.  
  60. Here are some examples                    *python-examples*  >
  61.  
  62.     :python from vim import *
  63.     :python from string import upper
  64.     :python current.line = upper(current.line)
  65.     :python print "Hello"
  66.     :python str = current.buffer[42]
  67.  
  68. (Note that changes - like the imports - persist from one command to the next,
  69. just like in the Python interpreter.)
  70.  
  71. ==============================================================================
  72. 2. The vim module                    *python-vim*
  73.  
  74. Python code gets all of its access to vim (with one exception - see
  75. |python-output| below) via the "vim" module. The vim module implements two
  76. methods, three constants, and one error object.
  77.  
  78. Overview >
  79.     print "Hello"            # displays a message
  80.     vim.command(cmd)        # execute an ex command
  81.     w = vim.windows[n]        # gets window "n"
  82.     cw = vim.current.window        # gets the current window
  83.     b = vim.buffers[n]        # gets buffer "n"
  84.     cb = vim.current.buffer        # gets the current buffer
  85.     w.height = lines        # sets the window height
  86.     w.cursor = (row, col)        # sets the window cursor position
  87.     pos = w.cursor            # gets a tuple (row, col)
  88.     name = b.name            # gets the buffer file name
  89.     line = b[n]            # gets a line from the buffer
  90.     lines = b[n:m]            # gets a list of lines
  91.     num = len(b)            # gets the number of lines
  92.     b[n] = str            # sets a line in the buffer
  93.     b[n:m] = [str1, str2, str3]    # sets a number of lines at once
  94.     del b[n]            # deletes a line
  95.     del b[n:m]            # deletes a number of lines
  96.  
  97. Methods
  98.     vim.command(str)                *python-command*
  99.     Executes the vim (ex-mode) command str. Returns None.
  100.     Examples: >
  101.         vim.command("set tw=72")
  102.         vim.command("%s/aaa/bbb/g")
  103. <    The following definition executes Normal mode commands: >
  104.         def normal(str):
  105.             vim.command("normal "+str)
  106.         # Note the use of single quotes to delimit a string containing
  107.         # double quotes
  108.         normal('"a2dd"aP')
  109. <
  110.     vim.eval(str)                    *python-eval*
  111.     Evaluates the expression str using the vim internal expression
  112.     evaluator (see |expression|). Returns the expression result as a
  113.     string.
  114.     Examples: >
  115.         text_width = vim.eval("'tw'")
  116.         str = vim.eval("12+12")        # NB result is a string! Use
  117.                         # string.atoi() to convert to
  118.                         # a number.
  119.  
  120. Error object
  121.     vim.error                    *python-error*
  122.     Upon encountering a Vim error, Python raises an exception of type
  123.     vim.error.
  124.     Example: >
  125.         try:
  126.             vim.command("put a")
  127.         except vim.error:
  128.             # nothing in register a
  129.  
  130. Constants
  131.     Note that these are not actually constants - you could reassign them.
  132.     But this is silly, as you would then lose access to the vim objects
  133.     to which the variables referred.
  134.  
  135.     vim.buffers                    *python-buffers*
  136.     A sequence object providing access to the list of vim buffers. The
  137.     object supports the following operations: >
  138.         b = vim.buffers[i]    # Indexing (read-only)
  139.         b in vim.buffers    # Membership test
  140.         n = len(vim.buffers)    # Number of elements
  141.         for b in vim.buffers:    # Sequential access
  142. <
  143.     vim.windows                    *python-windows*
  144.     A sequence object providing access to the list of vim windows. The
  145.     object supports the following operations: >
  146.         w = vim.windows[i]    # Indexing (read-only)
  147.         w in vim.windows    # Membership test
  148.         n = len(vim.windows)    # Number of elements
  149.         for w in vim.windows:    # Sequential access
  150. <
  151.     vim.current                    *python-current*
  152.     An object providing access (via specific attributes) to various
  153.     "current" objects available in vim:
  154.         vim.current.line    The current line (RW)        String
  155.         vim.current.buffer    The current buffer (RO)        Buffer
  156.         vim.current.window    The current window (RO)        Window
  157.         vim.current.range    The current line range (RO)    Range
  158.  
  159.     The last case deserves a little explanation. When the :python or
  160.     :pyfile command specifies a range, this range of lines becomes the
  161.     "current range". A range is a bit like a buffer, but with all access
  162.     restricted to a subset of lines. See |python-range| for more details.
  163.  
  164. Output from Python                    *python-output*
  165.     Vim displays all Python code output in the Vim message area.  Normal
  166.     output appears as information messages, and error output appears as
  167.     error messages.
  168.  
  169.     In implementation terms, this means that all output to sys.stdout
  170.     (including the output from print statements) appears as information
  171.     messages, and all output to sys.stderr (including error tracebacks)
  172.     appears as error messages.
  173.  
  174.                             *python-input*
  175.     Input (via sys.stdin, including input() and raw_input()) is not
  176.     supported, and may cause the program to crash. This should probably be
  177.     fixed.
  178.  
  179. ==============================================================================
  180. 3. Buffer objects                    *python-buffer*
  181.  
  182. Buffer objects represent vim buffers. You can obtain them in a number of ways:
  183.     - via vim.current.buffer (|python-current|)
  184.     - from indexing vim.buffers (|python-buffers|)
  185.     - from the "buffer" attribute of a window (|python-window|)
  186.  
  187. Buffer objects have one read-only attribute - name - the full file name for
  188. the buffer. They also have three methods (append, mark, and range; see below).
  189.  
  190. You can also treat buffer objects as sequence objects. In this context, they
  191. act as if they were lists (yes, they are mutable) of strings, with each
  192. element being a line of the buffer. All of the usual sequence operations,
  193. including indexing, index assignment, slicing and slice assignment, work as
  194. you would expect. Note that the result of indexing (slicing) a buffer is a
  195. string (list of strings). This has one unusual consequence - b[:] is different
  196. from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas
  197. "b = None" merely updates the variable b, with no effect on the buffer.
  198.  
  199. Buffer indexes start at zero, as is normal in Python. This differs from vim
  200. line numbers, which start from 1. This is particularly relevant when dealing
  201. with marks (see below) which use vim line numbers.
  202.  
  203. The buffer object methods are:
  204.     b.append(str)    Append a line to the buffer
  205.     b.append(list)    Append a list of lines to the buffer
  206.             Note that the option of supplying a list of strings to
  207.             the append method differs from the equivalent method
  208.             for Python's built-in list objects.
  209.     b.mark(name)    Return a tuple (row,col) representing the position
  210.             of the named mark (can also get the []"<> marks)
  211.     b.range(s,e)    Return a range object (see |python-range|) which
  212.             represents the part of the given buffer between line
  213.             numbers s and e (inclusive).
  214.  
  215. Examples (assume b is the current buffer) >
  216.     print b.name        # write the buffer file name
  217.     b[0] = "hello!!!"    # replace the top line
  218.     b[:] = None        # delete the whole buffer
  219.     del b[:]        # delete the whole buffer (same as above)
  220.     b[0:0] = "add a line"    # add a line at the top
  221.     del b[2]        # delete a line (the third)
  222.     b.append("bottom")    # add a line at the bottom
  223.     n = len(b)        # number of lines
  224.     (row,col) = b.mark('a') # named mark
  225.     r = b.range(1,5)    # a sub-range of the buffer
  226.  
  227. ==============================================================================
  228. 4. Range objects                    *python-range*
  229.  
  230. Range objects represent a part of a vim buffer. You can obtain them in a
  231. number of ways:
  232.     - via vim.current.range (|python-current|)
  233.     - from a buffer's range() method (|python-buffer|)
  234.  
  235. A range object is almost identical in operation to a buffer object. However,
  236. all operations are restricted to the lines within the range (this line range
  237. can, of course, change as a result of slice assignments, line deletions, or
  238. the range.append() method).
  239.  
  240. Unlike buffers, ranges do not have a "name" attribute, nor do they have mark()
  241. or range() methods. They do have an append() method, however, which adds
  242. line(s) to the end of the range.
  243.  
  244. ==============================================================================
  245. 5. Window objects                    *python-window*
  246.  
  247. Window objects represent vim windows. You can obtain them in a number of ways:
  248.     - via vim.current.window (|python-current|)
  249.     - from indexing vim.windows (|python-windows|)
  250.  
  251. You can manipulate window objects only through their attributes. They have no
  252. methods, and no sequence or other interface.
  253.  
  254. Window attributes are:
  255.     buffer (read-only)    The buffer displayed in this window
  256.     cursor (read-write)    The current cursor position in the window
  257.                 This is a tuple, (row,col).
  258.     height (read-write)    The window height, in rows
  259.     width (read-write)    The window width, in columns
  260. The height attribute is writable only if the screen is split horizontally.
  261. The width attribute is writable only if the screen is split vertically.
  262.  
  263. ==============================================================================
  264.  vim:tw=78:ts=8:ft=help:norl:
  265.